#pragma once
#ifndef MONSTER_H
#define MONSTER_H
#include <raylib.h>
#include "Sprite.h"
#include <map>
#include <ctime>
#include <chrono>
#include <vector>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <thread>
#include "Hero.h"
#include "TileMap.h"
#include "mapbase.h"  
#include "string"
using namespace std;

class GameScene;
class Monster:public sprite
{
    public :
        GameScene* gameScene=nullptr;
        Hero* hero=nullptr;
        TileMap* map;
        Vector2 position;
        int direction=3;
        int getRightDirection();
        Vector2 getPosition();
        vector<Rectangle> actionFrames;
        virtual vector<float> getCollisionBox();
        Rectangle getRectangle();
        float width=27.0f;
        float height=40.0f;
        int currentFrame = 0;
        float Velocity=100.0f;
        float originStep=deltaTime*Velocity;
        float step=deltaTime*Velocity;

        clock_t start;

        //最大追逐时间
        double chase_time=1.0f;
        int chase_direction=0;
        bool chasing=false;
        bool hitWalls=false;
        bool reachedPathEnd=false;
        std::vector<Coord<int>> chasePath;
        int chasePathIndex=0;
        int getChaseNextDirection();
        void updateNextChasePoint();
        void changeDirectionInChase();
        void useSimpleChaseAtEnd();

        void getToNearbyBlocksForChase();
        
        std::map<int,vector<Rectangle>> nearbyBlocks;
        // std::map<int,vector<Rectangle>> Blocks;
        void setGameScene(GameScene *gameScene);
        void setHero(Hero* hero){this->hero=hero;}
        void setMap(TileMap* map){this->map=map;}

        string imagePath="assets\\imgs\\characters\\slime.png";

        Monster() {
            vector<Rectangle> frames;
            for (int i = 0; i < 3; i++) {
                frames.push_back({
                    (float)(i * 40),  // X坐标
                    0.0f,            // Y坐标（默认方向）
                    40.0f,           // 宽度
                    40.0f            // 高度
                });
            }
            this->actionFrames = frames;
            chara_texture=initTexture(imagePath);
            start = clock();

        }
        ~Monster(){}
        Texture chara_texture;
        Texture initTexture(string imgPath);
        void changeDirection(int direction);
        void setInitialPosition(float x,float y);
        void updateFromGameScene(GameScene* scene) ;
        // void updateBlocksFromGameScene(std::map<int,vector<Rectangle>> blocks){this->Blocks=blocks;}

        //处理运动
        void handleMovement();
        void handleChangeDirection();
        std::pair<bool, int> existHeroInCR();

        void updateShowInformations();

        double levelTime=0.0f;
        void updateLevelTime(double time){levelTime=time;}
};
#endif